﻿using PGMHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HystereticBasic
{
    /// <summary>
    /// 延性对象集合
    /// </summary>
    public class DuctilityObjSet
    {
        /// <summary>
        /// 各构件的延性数据
        /// </summary>
        public List<DuctilityObj> DucObjList { set; get; }

        /// <summary>
        /// 初始化
        /// </summary>
        public void SetDuctilityFactor(float ultimateStrengthFactor, float parkFactor)
        {
            //遍历对象
            this.DucObjList.ForEach(obj => obj.SetDuctilityFactor(ultimateStrengthFactor, parkFactor));
        }

        /// <summary>
        /// 获得所有数据
        /// </summary>
        public GridViewDatas GetDuctilityData()
        {
            //初始化
            var data = new GridViewDatas("延性系数");
            //函数指针
            Action<DataType> addColumn = (dataType) =>
            {
                //获得值
                var valueList = (from obj in this.DucObjList select obj[dataType]).ToList();
                //整合数据列表
                data.Add(new GridViewColumn(valueList, demical: HystereticHelper.GetDemical(dataType),
                    titleName: HystereticHelper.GetDescp(dataType)));
            };
            //函数指针
            Action<DataType, DuctilityMethod> addColumnDuc = (dataType, method) =>
            {
                //获得值
                var valueList = (from obj in this.DucObjList select obj[dataType, method]).ToList();
                //整合数据列表
                data.Add(new GridViewColumn(valueList, demical: HystereticHelper.GetDemical(dataType),
                    titleName: HystereticHelper.GetDescp(dataType, method)));
            };
            //函数指针
            Action<DuctilityMethod> addColumnDucMethod = (method) =>
            {
                addColumnDuc(DataType.DuctilityYieldDisp, method);
                addColumnDuc(DataType.DuctilityYieldForce, method);
                addColumnDuc(DataType.DuctilityFactor, method);
            };
            //构件名称
            data.Add(new GridViewColumn(this.DucObjList.GetPropertyList("Name"), titleName: "名称"));
            //峰值点
            addColumn(DataType.DuctilityPeakDisp);
            addColumn(DataType.DuctilityPeakForce);
            //极限点
            addColumn(DataType.DuctilityUltimateDisp);
            addColumn(DataType.DuctilityUltimateForce);
            //屈服点
            addColumnDucMethod(DuctilityMethod.General);
            addColumnDucMethod(DuctilityMethod.Energy);
            addColumnDucMethod(DuctilityMethod.Park);
            addColumnDucMethod(DuctilityMethod.AVG);
            //返回数据
            return data;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DuctilityObjSet()
        {
            this.DucObjList = new List<DuctilityObj>();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="filePathList"></param>
        /// <param name="skipNum"></param>
        /// <param name="ultimateStrengthFactor"></param>
        /// <param name="parkFactor"></param>
        public DuctilityObjSet(List<string> filePathList, int skipNum, 
            float ultimateStrengthFactor, float parkFactor)
            :this()
        {
            //遍历文件
            foreach(var filePath in filePathList)
            {
                //获得时程
                var obj = new ForceDispObj(filePath, skipNum);
                //名称
                var name = Path.GetFileNameWithoutExtension(filePath);
                //加载卸载路径
                var cycleSet = new CycleSet(obj);
                //存在环
                if (cycleSet.Count == 0) continue;
                //获得第一个环
                var loadingOjb = cycleSet[0].GetLoadingPathForceDispObjWithoutUnload(true);
                //存在数据
                if (loadingOjb.Count == 0) continue;
                //构造延性系数
                var ducObj = new DuctilityObj(loadingOjb, name, ultimateStrengthFactor, parkFactor);
                //对象是否合法
                if (!ducObj.isLegal) continue;
                //添加对象
                this.DucObjList.Add(ducObj);
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dataSet"></param>
        /// <param name="isPos"></param>
        /// <param name="ultimateStrengthFactor"></param>
        /// <param name="parkFactor"></param>
        public DuctilityObjSet(HyDataSet dataSet, bool isPos,
            float ultimateStrengthFactor,
            float parkFactor)
            :this()
        {
            //遍历对象
            foreach (var data in dataSet.DataList)
            {
                //构造延性系数
                var ducObj = new DuctilityObj(data, isPos, ultimateStrengthFactor, parkFactor);
                //对象是否合法
                if (!ducObj.isLegal) continue;
                //添加对象
                this.DucObjList.Add(ducObj);
            }
        }
    }
}
